# coding = utf-8

'''
基于卷积的思想去计算灰度共生矩阵
'''

import cv2,os
import numpy as np
import matplotlib.pyplot as plt
import SimpleITK as sitk
from PIL import Image
import math

def specilization(image):
    image = image*255
    image = image.astype(np.int64)
    image = image / 8
    image = image.astype(np.uint8)
    return image


def glgc(image, a, b, instace_level=32):


    '''
    min_data = np.min(image)
    max_data = np.max(image)

    if max_data != min_data and (max_data-min_data) > instace_level - 1:
            image = (image-min_data) / (max_data-min_data)
            image = image*(instace_level-1)
            image = image.astype(np.uint8)
    else:
            image = image - min_data
    '''



    glgc_matrix = np.zeros((instace_level, instace_level))

    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            f1 = image[i][j]
            if i+a < 0 or i+a >= image.shape[0] or j + b < 0  or j + b >= image.shape[1]:
                continue
            else:
                f2 = image[i+a][j+b]
            glgc_matrix[f1][f2] += 1

    return glgc_matrix

def get_glgc_map(image, kernel, a, b):
    entropy_map = np.zeros((image.shape[0]//kernel, image.shape[1]//kernel))
    energy_map = np.zeros((image.shape[0]//kernel, image.shape[1]//kernel))
    contrast_map = np.zeros((image.shape[0]//kernel, image.shape[1]//kernel))
    differ_moment = np.zeros((image.shape[0]//kernel, image.shape[1]//kernel))
    print(entropy_map.shape)

    supply_matrix = np.zeros((32, 32))
    for i in range(supply_matrix.shape[0]):
        for j in range(supply_matrix.shape[1]):
            supply_matrix[i][j] = math.pow((i - j), 2)


    for i in range(0, image.shape[0]-kernel+1, kernel):
        for j in range(0, image.shape[1]-kernel+1, kernel):
            matrix1 = glgc(image[i: i + kernel + 1, j : j + kernel  + 1], a=a, b=b)
            matrix = matrix1 / matrix1.sum()

            entropy_map[i//kernel, j//kernel] = (-matrix * np.log(matrix + 0.0000001)).sum()
            energy_map[i//kernel, j//kernel] = np.power(matrix, 2).sum()
            contrast_map[i//kernel, j//kernel] = (supply_matrix * matrix).sum()
            differ_moment[i//kernel, j//kernel] = ((1 / (1 + supply_matrix)) * matrix).sum()

    return (entropy_map, energy_map, contrast_map, differ_moment)



def read_data():
    def find_data(case_id, origion_id, index):
         big_image = "E:\Dataset\Liver\qiye\DongBeiDaXue2\image_venous\\data2_{}_venous.mha".format(origion_id)
         big_liver = "E:\Dataset\Liver\qiye\DongBeiDaXue2\liver\\data2_{}_liver_label.mha".format(origion_id)
         big_tumor = "E:\Dataset\Liver\qiye\DongBeiDaXue2\lesion\\data2_{}_lesion_label.mha".format(origion_id)
         big_fusion = "E:\predict\image_tumor\case_{}\\fusion\\{}.png".format(str(case_id).zfill(5), str(index).zfill(3))
         big_image = sitk.GetArrayFromImage(sitk.ReadImage(big_image))
         big_image[big_image<=-200] = -200
         big_image[big_image > 250] = 250
         big_image = (big_image+200) / 450
         big_image = big_image[index]
         big_liver = sitk.GetArrayFromImage(sitk.ReadImage(big_liver))
         big_liver = big_liver[index]
         big_tumor = sitk.GetArrayFromImage(sitk.ReadImage(big_tumor))
         big_tumor = big_tumor[index]
         big_fusion = Image.open(big_fusion)
         return (big_image, big_fusion, big_liver, big_tumor)

    (big_image, big_fusion, big_liver, big_tumor) = find_data(case_id=74, origion_id="0656", index=40)
    (middle_image, middle_fusion, middle_liver, middle_tumor) = find_data(case_id=67, origion_id="0415", index=135)
    (small_image, small_fusion, small_liver, small_tumor) = find_data(case_id=79, origion_id="0865", index=262)
    (little_image, little_fusion, little_liver, little_tumor) = find_data(case_id=72, origion_id="0628", index=142)

    spec = specilization(big_image)
    (entropy_map2, energy_map2, contrast_map2, differ_moment2) = get_glgc_map(image=spec, kernel=2, a=0, b=1)
    entropy_map2[entropy_map2<=0] = 0

    (entropy_map4, energy_map4, contrast_map4, differ_moment4) = get_glgc_map(image=spec, kernel=4, a=0, b=1)
    entropy_map4[entropy_map4 <= 0] = 0

    (entropy_map8, energy_map8, contrast_map8, differ_moment8) = get_glgc_map(image=spec, kernel=8, a=0, b=1)
    entropy_map8[entropy_map8 <= 0] = 0

    (entropy_map16, energy_map16, contrast_map16, differ_moment16) = get_glgc_map(image=spec, kernel=16, a=0, b=1)
    entropy_map16[entropy_map16 <= 0] = 0

    plt.subplot(2, 3, 1)
    plt.imshow(big_image, cmap="gray")
    plt.subplot(2, 3, 2)
    plt.imshow(1-energy_map2, cmap="gray")
    plt.subplot(2, 3, 3)
    plt.imshow(1-energy_map4, cmap="gray")
    plt.subplot(2, 3, 4)
    plt.imshow(1-energy_map8, cmap="gray")
    plt.subplot(2, 3, 5)
    plt.imshow(1-energy_map16, cmap="gray")

    plt.show()



if __name__ == '__main__':
    read_data()